<?php
/**
 * CreateTime: 2022/4/30 14:10
 */

namespace App\Http\Controllers\Dictionary;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Dictionary\AddDictionaryPost;
use App\Service\Dictionary\DictionaryService;

class DictionaryController extends Controller
{
    private DictionaryService $dictionaryService;

    public function __construct()
    {
        $this->dictionaryService = new DictionaryService();
    }

    /**
     * 通过字典ID获取其子节点id
     * @param $dictionaryIds
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDictionaryNextByDictionaryIds($dictionaryIds): \Illuminate\Http\JsonResponse
    {
        $dictionaryIdsArr = explode(',',$dictionaryIds);
        return ResponseFunc::returnJson(
            'ok',
            $this->dictionaryService->getDictionaryNextByDictionaryIds($dictionaryIdsArr),
            '获取字典成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 获取数据字典
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDictionaryAllDictionary(): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->dictionaryService->getDictionaryAllDictionary(),
            '获取字典成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function addDictionary(AddDictionaryPost $addDictionaryPost){
        $addDictionaryPostData = $addDictionaryPost->all();
        return ResponseFunc::returnJson(
            'ok',
            [
                'dictionary_id' =>  $this->dictionaryService->addDictionary($addDictionaryPostData)
            ],
            '创建字典成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 编辑字典
     * @return \Illuminate\Http\JsonResponse
     */
    public function editDictionary($dictionaryId): \Illuminate\Http\JsonResponse
    {
        $dictionaryPostData = request()->all();
        $dictionaryPostData['dictionary_id'] = $dictionaryId;
        $this->dictionaryService->editDictionary($dictionaryPostData);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '编辑数据字典成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 删除字典
     * @param $dictionaryId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteDictionary($dictionaryId): \Illuminate\Http\JsonResponse
    {
        $dictionaryData = [
            'dictionary_id' => $dictionaryId,
            'status' => BaseConsts::STATUS_DELETE
        ];
        $this->dictionaryService->deleteDictionary($dictionaryData);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除数据字典成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }
}